package com.stormrage.edu.serv.edu.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.stormrage.constants.ErrCons;
import com.stormrage.edu.dao.edu.EduLecDao;
import com.stormrage.edu.serv.edu.EduCourseServ;
import com.stormrage.edu.serv.edu.EduLecServ;
import com.stormrage.entity.edu.EduLec;
import com.stormrage.entity.edu.rep.EduLecRep;
import com.stormrage.entity.edu.req.EduLecReq;
import com.stormrage.entity.rep.ListRep;
import com.stormragetech.core.exception.StormrageTechException;
import com.stormragetech.core.util.FileUtil;

/**
 * @Title: EduLecServImpl.java
 * @Package com.stormrage.edu.serv.edu.impl
 * @Description: 讲师 Service
 * @author www.stormragetech.com
 * @date 2016年4月10日
 * @version V1.0
 */
@Service
public class EduLecServImpl implements EduLecServ {

	private static final Logger logger = LoggerFactory.getLogger(EduLecServImpl.class);

	@Autowired
	private EduLecDao eduLecDao;

	@Autowired
	private EduCourseServ eduCourseServ;

	@Override
	public Integer create(final EduLec arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduLecServ 】- 【 Create 】- 开始运行 ");

		Integer index = eduLecDao.create(arg0);

		logger.debug(" 【 Serv 】- 【 EduLecServ 】- 【 Create 】- 完成运行 ");
		return index;
	}

	@Override
	public void update(final EduLec arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduLecServ 】- 【 Update 】- 开始运行 ");

		eduLecDao.update(arg0);

		logger.debug(" 【 Serv 】- 【 EduLecServ 】- 【 Update 】- 完成运行 ");
	}

	@Override
	public void updateIdx(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduLecServ 】- 【 updateIdx 】- 开始运行 ");

		EduLec entity = queryById(arg0);

		Integer idx = entity.getIdx().equals(0) ? 1 : 0;
		entity.setIdx(idx);

		eduLecDao.updateIdx(entity);

		logger.debug(" 【 Serv 】- 【 EduLecServ 】- 【 updateIdx 】- 完成运行 ");
	}

	@Override
	public void delete(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduLecServ 】- 【 Delete 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		Integer count = eduCourseServ.queryCount(arg0);
		if (count > 0) {
			throw StormrageTechException.instance(ErrCons.SERV_ERR, "课程关联此讲师，不能删除", StormrageTechException.ERR_APP,
					"【 EduLecServImpl 】-【 Delete 】");
		}

		eduLecDao.delete(map);

		logger.debug(" 【 Serv 】- 【 EduLecServ 】- 【 Delete 】- 完成运行 ");
	}

	@Override
	public Integer queryCount(final EduLecReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduLecServ 】- 【 QueryCount 】- 开始运行 ");

		Map<String, Object> map = initQuery(arg0);

		Integer index = eduLecDao.queryCount(map);

		logger.debug(" 【 Serv 】- 【 EduLecServ 】- 【 QueryCount 】- 完成运行 ");
		return index;
	}

	@Override
	public EduLec queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduLecServ 】- 【 QueryById 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		EduLec entity = eduLecDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 EduLecServ 】- 【 QueryById 】- 完成运行 ");
		return entity;
	}

	@Override
	public ListRep queryList(final EduLecReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduLecServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = initQuery(arg0);

		Integer count = eduLecDao.queryCount(map);

		List<EduLec> array = eduLecDao.queryList(map);

		List<EduLecRep> list = new ArrayList<EduLecRep>();
		int seq = 1;

		Iterator<EduLec> it = array.iterator();
		while (it.hasNext()) {
			EduLec lec = it.next();
			Integer courseCount = eduCourseServ.queryCount(lec.getId());
			EduLecRep lecRep = EduLecRep.instance(lec, seq, courseCount);
			list.add(lecRep);
			seq++;
		}

		ListRep rep = ListRep.instance(count, list);

		logger.debug(" 【 Serv 】- 【 EduLecServ 】- 【 QueryList 】- 完成运行 ");
		return rep;
	}

	@Override
	public List<EduLec> queryList() throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduLecServ 】- 【 QueryList 】- 开始运行 ");

		List<EduLec> array = eduLecDao.queryList();

		logger.debug(" 【 Serv 】- 【 EduLecServ 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	@Override
	public String upload(final MultipartFile arg0, final String arg1) throws StormrageTechException {

		logger.debug(" 【 Service 】- 【 EduCourseServ 】- 【 Upload 】- 【 STARTED 】");

		String fileName = null;

		fileName = FileUtil.saveFile(arg0, arg1, "image");

		logger.debug(" 【 Service 】- 【 EduCourseServ 】- 【 Upload 】- 【 COMPLETED 】");
		return fileName;
	}

	private Map<String, Object> initQuery(EduLecReq arg0) {

		if (null == arg0)
			return null;

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startPage", arg0.getPage().getStartPage());
		map.put("size", arg0.getPage().getSize());

		StringBuffer query = new StringBuffer("");

		if (null != arg0.getLec() && arg0.getLec().length() > 0) {
			query.append("lec LIKE :lec AND ");
			map.put("lec", "%" + arg0.getLec() + "%");
		}

		String sql = query.toString();
		if (null != sql && sql.length() > 0)
			map.put("query", " WHERE " + sql.substring(0, sql.length() - 4));
		else
			map.put("query", "");

		return map;
	}

}
